import React, { Component } from 'react'
import { Form, Input, message } from 'antd'
import { fetchPost } from '../../../utils/request'

const FormItem = Form.Item
const formItemLayout = {labelCol: {span: 8}, wrapperCol: {span: 10}}
class FundAction extends Component {
  constructor (props) {
    super(props)
    this.state = {
      action: props.action,
      cashType: props.cashType,
      balance: props.balance
    }
  }
  componentWillReceiveProps (props) {
    if (props.action !== this.state.action || props.cashType !== this.state.cashType) {
      this.setState({
        action: props.action,
        cashType: props.cashType,
        balance: props.balance
      })
    }
  }
  componentDidMount () {
    this.props.triggerRef(this)
  }

  doFundAction () {
    let pass = false
    let self = this
    this.props.form.validateFields((err, values) => {
      if (!err) {
        pass = true
        let handleMoney = values.handleMoney ? values.handleMoney : values.withdraw
        fetchPost('/account/handle', {
          'cashType': self.state.cashType,
          'handleMoney': handleMoney
        }).then((res) => {
          this.setState({
            tableLoading: false
          })
          if (res.code === 0) {
            message.info(res.msg)
          } else {
            message.error(res.msg)
          }
          this.props.form.resetFields()
        })
      } else {
        pass = false
      }
    })
    return pass
  }
  getMainContent () {
    const { getFieldDecorator } = this.props.form
    if (this.state.action === '转账') {
      return (
        <div>
          <FormItem {...formItemLayout} label='转入账号' >
            <Input disabled value='捷融资金账号' />
          </FormItem>
          <FormItem {...formItemLayout} label='转入金额' >
            {getFieldDecorator('handleMoney',
              { rules: [
                  { required: true, message: '转入金额不能为空' }
              ]
              })(<Input />)}
          </FormItem>
        </div>
      )
    }
    if (this.state.action === '提现') {
      return (<FormItem {...formItemLayout} label='提现金额' >
        {getFieldDecorator('withdraw',
          { rules: [
              { required: true, message: '提现不能为空' }
          ]
          })(<Input />)}
      </FormItem>
      )
    }
  }
  render () {
    return (
      <Form layout='horizontal'>
        <FormItem {...formItemLayout} label='账户余额(元)' >
          <Input disabled value={this.state.balance} />
        </FormItem>
        {this.getMainContent()}
      </Form>
    )
  }
}
export default Form.create()(FundAction)
